/*******************************************************************************
 * Copyright (c) 2008 L.Carbonnaux.
 *******************************************************************************/
package org.lcx.taskvision.core.model;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.lcx.taskvision.core.TaskVisionCorePlugin;

import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.WorksheetEntry;

/**
 * @author Laurent Carbonnaux
 */
public class TaskVisionSpreadSheet {

	private String key;
	private String name;
	private URL worksheetFeedUrl;

	private List<TaskVisionWorkSheet> worksheets = new ArrayList<TaskVisionWorkSheet>();

	public TaskVisionSpreadSheet(SpreadsheetEntry entry) throws MalformedURLException {
	    String result = entry.getHtmlLink().getHref();
	    int position = result.lastIndexOf("=");
	    if (position > 0) {
	      result = result.substring(position + 1);
	    }
		this.key = result;
		
//		System.out.println("sp: id="+entry.getId()+" key="+entry.getKey()+", worksheetFeedUrl="+entry.getWorksheetFeedUrl());
		this.name = entry.getTitle().getPlainText();
		URL worksheetFeedUrl = TaskVisionCorePlugin.getFeedURLFactory().getWorksheetFeedUrl(this.key, "private", "full");
		this.worksheetFeedUrl = worksheetFeedUrl;
	}

	public TaskVisionSpreadSheet(DocumentListEntry entry) throws MalformedURLException {
		this.key = entry.getDocId();
//		System.out.println("sp: id="+entry.getId()+" key="+entry.getKey()+", docId="+entry.getDocId()+", resId="+entry.getResourceId());
		this.name = entry.getTitle().getPlainText();
		URL worksheetFeedUrl = TaskVisionCorePlugin.getFeedURLFactory().getWorksheetFeedUrl(this.key, "private", "full");
		this.worksheetFeedUrl = worksheetFeedUrl;
	}
	
	public void addWorksheet(WorksheetEntry worksheetEntry) {
		TaskVisionWorkSheet worksheet = new TaskVisionWorkSheet(worksheetEntry);
		worksheets.add(worksheet);
	}
	
	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<TaskVisionWorkSheet> getWorksheets() {
		return worksheets;
	}

	public void setWorksheets(List<TaskVisionWorkSheet> worksheets) {
		this.worksheets = worksheets;
	}

	public URL getWorksheetFeedUrl() {
		return worksheetFeedUrl;
	}

	public void setWorksheetFeedUrl(URL worksheetFeedUrl) {
		this.worksheetFeedUrl = worksheetFeedUrl;
	}

}
